import { Globals } from './../../../shared/global';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { NzModalService } from 'ng-zorro-antd';

@Component({
  selector: 'ree-create-product',
  templateUrl: './create-product.component.html',
  styleUrls: ['./create-product.component.scss']
})
export class CreateProductComponent implements OnInit {

  validateForm: FormGroup;

  nzLoading = false;

  _submitForm() {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
    }
    this.nzLoading = true;
    setTimeout(this.showModal(), 1000)
  }

  showModal() {
    this.nzLoading = false;
    this.confirmServ.confirm({
      content: '新建成功,是否前往添加设备？',
      okText: '是',
      cancelText: '否',
      iconType: 'check',
      onOk: this.skipToCreateDevice
    });
  }

  skipToCreateDevice() {
    console.log("跳转成功")
  }

  selectedOption;
  searchOptions = [
    { value: '空压机', label: '空压机' },
    { value: '干燥机', label: '干燥机' },
    { value: '冷冻机', label: '冷冻机' }
  ];
  confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (control.value !== this.validateForm.controls['password'].value) {
      return { confirm: true, error: true };
    }
  };

  ngOnInit() {
    this.validateForm = this.fb.group({
      email: [null, [Validators.email]],
      companyAd: [null, [Validators.required]],
      checkPassword: [null, [Validators.required, this.confirmationValidator]],
      companyName: [null, [Validators.required]],
      phoneNumberPrefix: ['+86'],
      phoneNumber: [null, [Validators.required]],
      website: [null, [Validators.required]],
      captcha: [null, [Validators.required]],
      agree: [false]
    });
  }

  getFormControl(name) {
    return this.validateForm.controls[name];
  }

  constructor(private global: Globals, private fb: FormBuilder, private confirmServ: NzModalService) { }

  ngOnDestroy() {
    this.global.paths.path.pop()
  }


}
